//
//  AppDelegate.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/21.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setupAdditions()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = MainViewController()
//        let vc = MainViewController()
//        vc.aaa = "aaaa"
//        print(vc.aaa ?? "")
        window?.makeKeyAndVisible()
        loadAPPIndo()
        return true
    }
}

extension AppDelegate {
    
    fileprivate func setupAdditions() {
        SVProgressHUD.setMinimumDismissTimeInterval(1.0)
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
                //                print(success ? "成功" : "失败")
            }
        } else {
            
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
    }
    
}

extension AppDelegate {
    fileprivate func loadAPPIndo() {
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            
            data?.write(toFile: jsonPath, atomically: true)
//            print(jsonPath)
        }
        
    }
}
